
clear all 
set more off 
set maxvar 15000 
clear matrix


**********************************************************************************************************************************************
**********************************************************************************************************************************************
**** IGE AND RANK OVER TIME FOR FOUR RACE X SEX GROUPS 
**********************************************************************************************************************************************
**********************************************************************************************************************************************
    
*-----------*
* IGE    
*-----------*

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    keep if decade>=1910 & decade<=1940 
    
	* Generate interaction term for decline
    sort dob
    egen time = group(dob)
    replace time = time-1   
    
    gen interaction = log_father_baseline*time 
    
    reg log_son_baseline log_father_baseline interaction i.time if race==1 & sex==1  [aw=wgt_sex_race], robust
        local temp =_se[interaction]
        local se00: display %-06.4fc `temp'
        
    reg log_son_baseline log_father_baseline interaction i.time [aw=wgt_sex_race], robust   
        local temp =_se[interaction]
        local se11: display %-06.4fc `temp'
    
    reg log_son_baseline log_father_baseline interaction i.time if race==1 & sex==1  [aw=wgt_sex_race] 
    est store whitemen    
        local number0 =_b[interaction]
        local number00: display %-06.4fc `number0'

    reg log_son_baseline log_father_baseline interaction i.time [aw=wgt_sex_race] 
    est store all    
        local number1 =_b[interaction]
        local number11: display %-06.4fc `number1'  

    eststo: suest whitemen all 
    
    test [whitemen_mean]interaction=[all_mean]interaction
    
        local diff0 =`r(p)'
        local diff00: display %-04.2fc `diff0'
        display `diff00'
        
*--------------*
* RANK-RANK   
*--------------*

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    keep if decade>=1910 & decade<=1940 
	
    * Generate interaction term for decline
    sort dob
    egen time = group(dob)
    replace time = time-1   
        
    gen interaction = rank_father_baseline*time	
    
    
    reg rank_son_baseline rank_father_baseline interaction i.time if race==1 & sex==1  [aw=wgt_sex_race], robust    
        local temp =_se[interaction]
        local se22: display %-06.4fc `temp'
    
    reg rank_son_baseline rank_father_baseline interaction i.time [aw=wgt_sex_race], robust    
        local temp =_se[interaction]
        local se33: display %-06.4fc `temp'
    
    reg rank_son_baseline rank_father_baseline interaction i.time if race==1 & sex==1  [aw=wgt_sex_race]
    est store whitemen
        local number2 =_b[interaction]
        local number22: display %-06.4fc `number2'  
    
    reg rank_son_baseline rank_father_baseline interaction i.time [aw=wgt_sex_race]
    est store all    
        local number3 =_b[interaction]
        local number33: display %-06.4fc `number3'

    eststo: suest whitemen all
    
    test [whitemen_mean]interaction=[all_mean]interaction
    
        local diff1 =`r(p)'
        local diff11: display %-04.2fc `diff1'
        display `diff11'

    
* Figures 

    foreach m in rank log   {

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
        keep if baseline_sample==1
        
        if "`m'"=="log" local dep "log_son_baseline"
        if "`m'"=="rank" local dep "rank_son_baseline"
        
        if "`m'"=="log" local indep "log_father_baseline"
        if "`m'"=="rank" local indep "rank_father_baseline"

        forval i=1(1)4 {
            gen est_`i'=. 
            gen est_lb_`i' =.
            gen est_ub_`i' =.
        }
        
        levelsof decade, local(decades) 

    *1. 
        foreach x of local decades {
            di "`x'"
            quietly reg `dep' `indep'  if race==1 & sex==1 & decade==`x' [pw=wgt_sex_race], robust 
                replace est_1 = _b[`indep'] if decade==`x'
                replace est_ub_1 = _b[`indep']+1.96*_se[`indep'] if decade==`x'
                replace est_lb_1 = _b[`indep']-1.96*_se[`indep'] if decade==`x'
        }

    *2. 
        foreach x of local decades {
            di "`x'"
            quietly  reg `dep' `indep'  if race==1 & decade==`x' [pw=wgt_sex_race], robust  
                replace est_2 = _b[`indep'] if decade==`x'
                replace est_ub_2 = _b[`indep']+1.96*_se[`indep'] if decade==`x'
                replace est_lb_2 = _b[`indep']-1.96*_se[`indep'] if decade==`x'
        }
            
    *3. 
        foreach x of local decades {
            di "`x'"
            quietly  reg `dep' `indep'  if (race==1 | (race==2 & sex==1)) & decade==`x' [pw=wgt_sex_race], robust 
                replace est_3 = _b[`indep'] if decade==`x'
                replace est_ub_3 = _b[`indep']+1.96*_se[`indep'] if decade==`x'
                replace est_lb_3 = _b[`indep']-1.96*_se[`indep'] if decade==`x'
        }
            
    *. 
        foreach x of local decades {
            di "`x'"
            quietly reg `dep' `indep' if decade==`x' [pw=wgt_sex_race], robust  
                replace est_4 = _b[`indep'] if decade==`x'
                replace est_ub_4 = _b[`indep']+1.96*_se[`indep'] if decade==`x'
                replace est_lb_4 = _b[`indep']-1.96*_se[`indep'] if decade==`x'
        }
        
        bysort decade: keep if _n==1
        keep decade est_*
        
        reshape long est_ est_lb_ est_ub_, i(decade) j(estimate)
        replace decade= decade+1.5 if estimate==2
        replace decade= decade+3 if estimate==3
        replace decade= decade+4.5 if estimate==4
        
        if "`m'"=="log" {
            local title "IGE"
            local ub "1"
            local range "0.25"
            local lb "0"
            local pos "8"
        }
        
        if "`m'"=="rank" {
            local title "Rank"
            local ub "0.5"
            local range "0.1"
            local lb "0.1"
            local pos "8"
        }
        
        
        if "`m'"=="log" {

            #delimit ;
            twoway (scatter est_ decade if estimate==1,  msymbol(circle) mcolor(blue) msize(medium) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(blue) yaxis(1) lwidth(0.5))
                   (scatter est_ decade if estimate==2,  msymbol(triangle) mcolor(purple) msize(small) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(purple) yaxis(1) lwidth(0.5) )
                    (scatter est_ decade if estimate==3,  msymbol(circle_hollow) mcolor(red) msize(small) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==3, lpatter(solid) lcolor(red) yaxis(1) lwidth(0.5) )
                    (scatter est_ decade if estimate==4,  msymbol(triangle_hollow) mcolor(orange*0.7) msize(small) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==4, lpatter(solid) lcolor(orange*0.7) yaxis(1) lwidth(0.5) )
                   ,
            xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
            yscale(range(`lb' `ub')) ylabel(`lb'(`range')`ub', axis(1)) yti("`title' coefficient" " ", axis(1)) 
            legend(on ring(0) row(4) pos(`pos') order(1 "White men" 3 "+ White women" 5 "+ Black men" 7 "+ Black women"))
            xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) )
            text(0.15 1953 "White men, 1910-1940 decline: `number00' (`se00')", size(medsmall))
            text(0.1 1949.75 "All, 1910-1940 decline: `number11' (`se11')", size(medsmall))
            text(0.05 1943.75 "P-value of difference: `diff00'", size(medsmall));    
            #delimit cr
	graph export "$Mydirectory2/main_figures_tables/figure8_ige.pdf", as(pdf) replace
        
        }    
        
        if "`m'"=="rank" {

            #delimit ;
            twoway (scatter est_ decade if estimate==1,  msymbol(circle) mcolor(blue) msize(medium) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(blue) yaxis(1) lwidth(0.5))
                   (scatter est_ decade if estimate==2,  msymbol(triangle) mcolor(purple) msize(small) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(purple) yaxis(1) lwidth(0.5) )
                    (scatter est_ decade if estimate==3,  msymbol(circle_hollow) mcolor(red) msize(small) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==3, lpatter(solid) lcolor(red) yaxis(1) lwidth(0.5) )
                    (scatter est_ decade if estimate==4,  msymbol(triangle_hollow) mcolor(orange*0.7) msize(small) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==4, lpatter(solid) lcolor(orange*0.7) yaxis(1) lwidth(0.5) )
                   ,
            xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
            yscale(range(`lb' `ub')) ylabel(`lb'(`range')`ub', axis(1)) yti("`title' coefficient" " ", axis(1)) 
            legend(on ring(0) row(2) pos(`pos') order(1 "White men" 3 "+ White women" 5 "+ Black men" 7 "+ Black women"))
            xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) )
            text(0.47 1953 "White men, 1910-1940 decline: `number22' (`se22')", size(medsmall))
            text(0.45 1949.8 "All, 1910-1940 decline: `number33' (`se33')", size(medsmall))
            text(0.43 1943.6 "P-value of difference: `diff11'", size(medsmall));  
            #delimit cr
	graph export "$Mydirectory2/main_figures_tables/figure8_rank.pdf", as(pdf) replace
            
        }   
         
    }
